package org.lanqiao.wxq.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Cascade;
import org.lanqiao.tools.BaseServlet;
import org.lanqiao.wxq.dao.CascadeDao;
import org.lanqiao.wxq.dao.impl.CascadeDaoImpl;

import com.google.gson.Gson;

@WebServlet("/CascadeServlet")
public class CascadeServlet extends BaseServlet {
	
	CascadeDao dao = new CascadeDaoImpl();
	Gson gson= new Gson();

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.request = request;
		this.response = response;
		
		String type= getString("type");//获取type键的值
		redata.setMsg("");
		
		if (!isNull("type")) {	//"type"键
			
			switch (type) {		//type值
				case "selectBypositionId":
					selectBypositionId();
					break;
				case "selectAll":
					selectAll();
					break;
	//			case "insert":
	//				insert();	
				default:
					break;
			}
		}
		else {
			redata.setCode(3);
			redata.setMsg("type参数为空");
			redata.setData("");
		}
		response.getWriter().write(gson.toJson(redata));	//把redata对象的所有信息打印
	
	}

//		private void insert() {
//		// TODO Auto-generated method stub
//		
//	}

		private void selectAll() {

			List<Cascade> selectCascadeAll = dao.selectCascadeAll();
			
			redata.setData(selectCascadeAll);
			redata.setCode(2);
			redata.setMsg("selectAll成功");
	}

		private void selectBypositionId() {
			System.out.println("selectBypositionId");
			Integer positionId= getInt("positionId");
			if (redata.getMsg()!="") {
				redata.setCode(3);
				redata.setData("");
				return;
			}
			
			List<Cascade> selectCascadeBypositionId = dao.selectCascadeBypositionId(positionId);
			redata.setData(selectCascadeBypositionId);
			redata.setCode(2);
			redata.setMsg("selectBypositionId成功");
	}

}



















